#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试版MCP计算器服务测试脚本
"""

import asyncio
import traceback
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def debug_test():
    """调试测试MCP服务器功能"""
    server_params = StdioServerParameters(
        command="python",
        args=["main.py"]
    )
    
    try:
        print("=== 开始MCP服务器调试测试 ===")
        print("正在启动服务器...")
        
        async with stdio_client(server_params) as (read, write):
            print("服务器连接成功")
            async with ClientSession(read, write) as session:
                print("正在初始化会话...")
                await session.initialize()
                print("会话初始化成功")
                
                # 测试简单的加法运算
                print("\n测试加法运算:")
                try:
                    result = await session.call_tool("add", {"a": 5, "b": 3})
                    print(f"5 + 3 = {result.content[0].text}")
                except Exception as e:
                    print(f"加法测试失败: {e}")
                    traceback.print_exc()
                
                # 测试问候功能
                print("\n测试问候功能:")
                try:
                    result = await session.call_tool("say_hello", {"name": "测试用户"})
                    print(f"问候结果: {result.content[0].text}")
                except Exception as e:
                    print(f"问候测试失败: {e}")
                    traceback.print_exc()
                
                print("\n=== 调试测试完成 ===")
                
    except Exception as e:
        print(f"❌ 调试测试出错: {e}")
        traceback.print_exc()

if __name__ == "__main__":
    asyncio.run(debug_test())